package com.leosam.tvbox.mv.utils;

import java.util.List;

import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.net.HostAndPort;
import io.vertx.ext.web.RoutingContext;

/**
 * @author admin
 * @since 2023/6/12 21:16
 */
public class VertxUtils {

    private static ThreadLocal<String> host = new ThreadLocal<>();

    public static String queryParam(RoutingContext context, String wd) {
        List<String> param = context.queryParam(wd);
        if (param != null && param.size() > 0) {
            return param.get(0);
        }
        return "";
    }

    public static void update(RoutingContext context) {
        HttpServerRequest request = context.request();
        HostAndPort authority = request.authority();
        if (authority.port() != -1) {
            host.set(authority.host() + ":" + authority.port());
        } else {
            host.set(authority.host());
        }
    }

    public static String getHost() {
        return host.get();
    }

    public static String getMediaUrl() {
        return "http://" + getHost() + "/vod/media";
    }

    public static String getProxyUrl() {
        return "http://" + getHost() + "/vod/proxy";
    }
}
